﻿using DG.Tweening;
using Miao.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Miao.UI
{
    /// <summary>
    /// 恐怖效果：不断晃动并逐渐放大，营造紧张恐怖的氛围
    /// </summary>
    public class HorrorShakeScaleUIMotion : BaseUIMotion
    {
        float shakeDuration;       // 单次晃动持续时间
        float shakeStrength;       // 晃动强度
        float scaleSpeed;          // 放大速度
        float maxScale;            // 最大缩放倍数
        int shakeVibrato;          // 晃动次数
        bool enableRandomness;     // 是否启用随机效果

        private Sequence horrorSequence;
        private Vector3 originalPosition;
        private Vector3 originalScale;
        private bool isPlaying = false;

        public HorrorShakeScaleUIMotion(float shakeDuration = 0.8f, float shakeStrength = 30f,
            float scaleSpeed = 2f, float maxScale = 3f, int shakeVibrato = 10, bool enableRandomness = true)
        {
            this.shakeDuration = shakeDuration;
            this.shakeStrength = shakeStrength;
            this.scaleSpeed = scaleSpeed;
            this.maxScale = maxScale;
            this.shakeVibrato = shakeVibrato;
            this.enableRandomness = enableRandomness;
        }

        public override void Init()
        {
            base.Init();
            if (TweenGameObject != null)
            {
                originalPosition = TweenGameObject.transform.localPosition;
                originalScale = TweenGameObject.transform.localScale;
            }
        }

        public override void Play(bool forward, TweenCallback complete)
        {
            if (forward)
            {
                PlayForward(complete);
            }
            else
            {
                PlayBackward(complete);
            }
        }

        private void PlayForward(TweenCallback complete)
        {
            if (isPlaying) return;

            isPlaying = true;

            // 保存原始状态
            originalPosition = TweenGameObject.transform.localPosition;
            originalScale = TweenGameObject.transform.localScale;

            // 创建恐怖效果序列
            horrorSequence = DOTween.Sequence();

            // 第一阶段：轻微晃动预热
            horrorSequence.Append(CreateShakePhase(0.3f, shakeStrength * 0.3f, 5));

            // 第二阶段：开始加强晃动和轻微放大
            horrorSequence.Append(CreateIntensifyPhase());

            // 第三阶段：持续的恐怖循环
            horrorSequence.Append(CreateHorrorLoop());

            horrorSequence.OnComplete(() => {
                isPlaying = false;
                complete?.Invoke();
            });
        }

        private void PlayBackward(TweenCallback complete)
        {
            // 停止当前动画
            horrorSequence?.Kill();
            isPlaying = false;

            // 创建恢复动画
            Sequence restoreSequence = DOTween.Sequence();

            // 逐渐减弱晃动
            restoreSequence.Append(TweenGameObject.transform.DOShakePosition(0.5f, shakeStrength * 0.5f, 5)
                .SetEase(Ease.OutQuad));

            // 恢复原始大小和位置
            restoreSequence.Join(TweenGameObject.transform.DOScale(originalScale, 0.8f)
                .SetEase(Ease.OutBack));

            restoreSequence.Append(TweenGameObject.transform.DOLocalMove(originalPosition, 0.3f)
                .SetEase(Ease.OutQuad));

            restoreSequence.OnComplete(complete);
        }

        private Tween CreateShakePhase(float duration, float strength, int vibrato)
        {
            return TweenGameObject.transform.DOShakePosition(duration, strength, vibrato, 90f, false, true)
                .SetEase(Ease.InOutSine);
        }

        private Tween CreateIntensifyPhase()
        {
            // 创建并行动画组
            Sequence intensifySequence = DOTween.Sequence();

            // 加强晃动
            intensifySequence.Join(TweenGameObject.transform.DOShakePosition(1.2f, shakeStrength * 0.7f, shakeVibrato)
                .SetEase(Ease.InOutQuad));

            // 开始放大
            intensifySequence.Join(TweenGameObject.transform.DOScale(originalScale * 1.5f, 1.2f)
                .SetEase(Ease.OutQuad));

            return intensifySequence;
        }

        private Tween CreateHorrorLoop()
        {
            Sequence loopSequence = DOTween.Sequence();

            // 创建恐怖循环 - 重复5次
            for (int i = 0; i < 5; i++)
            {
                float currentIntensity = 1f + (i * 0.3f); // 逐渐加强
                float currentScale = 1.5f + (i * 0.3f);   // 逐渐放大

                // 强烈晃动
                loopSequence.Append(TweenGameObject.transform.DOShakePosition(shakeDuration,
                    shakeStrength * currentIntensity, shakeVibrato + i * 2, 90f, false, true)
                    .SetEase(enableRandomness ? Ease.InOutBounce : Ease.InOutSine));

                // 同时进行缩放震颤
                loopSequence.Join(TweenGameObject.transform.DOShakeScale(shakeDuration,
                    Vector3.one * 0.2f * currentIntensity, 8, 90f, false)
                    .SetEase(Ease.InOutQuad));

                // 逐渐放大
                loopSequence.Join(TweenGameObject.transform.DOScale(originalScale * Math.Min(currentScale, maxScale),
                    shakeDuration).SetEase(Ease.OutSine));

                // 随机暂停增加不可预测性
                if (enableRandomness)
                {
                    float randomPause = UnityEngine.Random.Range(0.1f, 0.4f);
                    loopSequence.AppendInterval(randomPause);
                }
            }

            // 最终爆发 - 最强烈的效果
            loopSequence.Append(CreateFinalBurst());

            return loopSequence;
        }

        private Tween CreateFinalBurst()
        {
            Sequence burstSequence = DOTween.Sequence();

            // 极度晃动
            burstSequence.Append(TweenGameObject.transform.DOShakePosition(1.5f,
                shakeStrength * 2f, shakeVibrato * 2, 90f, false, true)
                .SetEase(Ease.InOutElastic));

            // 同时旋转晃动
            burstSequence.Join(TweenGameObject.transform.DOShakeRotation(1.5f,
                Vector3.forward * 45f, 10, 90f, false)
                .SetEase(Ease.InOutSine));

            // 放大到最大
            burstSequence.Join(TweenGameObject.transform.DOScale(originalScale * maxScale, 1.5f)
                .SetEase(Ease.OutBounce));

            // 添加颜色闪烁效果（如果有CanvasGroup组件）
            var canvasGroup = TweenGameObject.GetComponent<CanvasGroup>();
            if (canvasGroup != null)
            {
                burstSequence.Join(canvasGroup.DOFade(0.3f, 0.1f).SetLoops(15, LoopType.Yoyo)
                    .SetEase(Ease.Flash));
            }

            return burstSequence;
        }

        // 手动停止动画的方法
        public void StopHorrorEffect()
        {
            horrorSequence?.Kill();
            isPlaying = false;

            // 恢复原始状态
            if (TweenGameObject != null)
            {
                TweenGameObject.transform.localPosition = originalPosition;
                TweenGameObject.transform.localScale = originalScale;
                TweenGameObject.transform.localRotation = Quaternion.identity;
            }
        }

        // 检查是否正在播放
        public bool IsPlaying => isPlaying;
    }
}
